package de.uni_mannheim.informatik.ai.dame_ai.dame.ui;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;

import de.uni_mannheim.informatik.ai.dame_ai.dame.application.DameFacade;
import de.uni_mannheim.informatik.ai.dame_ai.dame.domain.Player;
import de.uni_mannheim.informatik.ai.dame_ai.main.application.GameSettings;
import de.uni_mannheim.informatik.ai.dame_ai.main.application.MainPlayer;
import de.uni_mannheim.informatik.ai.dame_ai.util.CommonMenuBar;
import de.uni_mannheim.informatik.ai.dame_ai.util.CommonMenuBarListener;
import de.uni_mannheim.informatik.ai.dame_ai.util.FileSystem;
import de.uni_mannheim.informatik.ai.dame_ai.util.LoadGameDialog;
import de.uni_mannheim.informatik.ai.dame_ai.util.SaveGameDialog;




public class DameGuiFrame extends JFrame implements WindowStateListener, ComponentListener, CommonMenuBarListener
{
	DameGui checkers;
	private CommonMenuBar commonMenuBar= new CommonMenuBar();
	private static DameGuiFrame frame;
	private DameFacade app;
	/**
	 * @param bounds
	 */
	public DameGuiFrame(Rectangle bounds, Player[] players, GameSettings data) {
		
		setBounds(bounds.x,bounds.y,bounds.width,bounds.height);
		app = new DameFacade(players, data);
		checkers = new DameGui(bounds.width,bounds.height, players, app, data); 
		app.addObserver(checkers);
		checkers.recalulateBounds();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 //content panes must be opaque
	    this.getContentPane().add(checkers);
	    commonMenuBar.addCommonMenuBarListener(this);
	    this.setJMenuBar(commonMenuBar);
	    this.addComponentListener(this);

	}

	/*@Override
	public void paint(Graphics g) {
		checkers.paint(g);
	}*/
	public static void createAndShowGUI(GameSettings data,Rectangle bounds, WindowListener adapter) {
		MainPlayer[] players = data.getPlayers();
		System.out.println("Player number: "+players[0].getNumber()+" Name: "+players[0].getName()+" isHuman: "+players[0].isHuman());
		System.out.println("Player number: "+players[1].getNumber()+" Name: "+players[1].getName()+" isHuman: "+players[1].isHuman());
		Player[] damePlayers = new Player[2];
		damePlayers[0]= new Player(players[0].getNumber(),players[0].getNumber(),players[0].getName(),players[0].isHuman());
		damePlayers[1]= new Player(players[1].getNumber(),players[1].getNumber(),players[1].getName(),players[1].isHuman());
		frame = new DameGuiFrame(bounds, damePlayers, data);
		frame.setBackground(Color.gray);
		frame.addWindowStateListener(frame);
		frame.addWindowListener(adapter);
		frame.setVisible(true);
		
		
	 }
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               //createAndShowGUI(null,new Rectangle(5,5,500,500));
           }
		}
		);		
	}
	@Override
	public void windowStateChanged(WindowEvent arg0) {
		
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
	           public void run() {
	        	checkers.recalulateBounds();
	        	checkers.repaint();
	           }
			}
		);
		
	}
	@Override
	public void componentHidden(ComponentEvent arg0) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
	           public void run() {
	        	checkers.recalulateBounds();
	        	checkers.repaint();
	           }
			}
		);
		
	}
	@Override
	public void componentMoved(ComponentEvent arg0) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
	           public void run() {
	        	checkers.recalulateBounds();
	        	checkers.repaint();

	           }
			}
		);
		
	}
	@Override
	public void componentResized(ComponentEvent arg0) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
	           public void run() {
	        	checkers.recalulateBounds();
	        	checkers.repaint();

	           }
			}
		);
		
	}
	@Override
	public void componentShown(ComponentEvent arg0) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
	           public void run() {
	        	checkers.recalulateBounds();
	        	checkers.repaint();

	           }
			}
		);
		
	}

	@Override
	public void exit() {
		this.dispose();		
	}

	@Override
	public void loadGame() {
		File destFile=LoadGameDialog.askForFile(this, FileSystem.GameName.dame);
		app.loadGameFrom(destFile);
		
	}

	@Override
	public void newGame() {
		this.dispose();	
		
	}

	@Override
	public void quickLoad() {
		app.quickLoad();
		
	}

	@Override
	public void quickSave() {
		app.quickSave();
		
	}

	@Override
	public void restart() {
		app.restart();
		
	}

	@Override
	public void saveGame() {
		File destFile=SaveGameDialog.askForFile(this, FileSystem.GameName.dame);
		//File destFile = new File("test.xml");
		app.saveGame(destFile);
	}

	@Override
	public void showInfo() {
		//HelpFile.getHelp();
		
	}

	@Override
	public void showRules() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void replayCurrent() {
		app.replayCurrent();
		
	}

	@Override
	public void replaySaved() {
		File destFile=LoadGameDialog.askForFile(this, FileSystem.GameName.dame);
		app.replaySaved(destFile);
		
	}

	
    

}
